<?php

declare(strict_types=1);

namespace app\admin\command;

use app\common\model\BasketballCompetitions;
use app\common\model\BasketballCompetitionTable;
use app\common\model\FootballCompetitions;
use app\common\model\FootballCompetitionTable;
use app\common\model\MatchesSeason;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class Competition extends Command
{
    protected $model;
    //数据模块的排行榜   每天一次就行
    protected function configure()
    {
        // 指令配置
        $this->setName('competition')
            ->addArgument('action', Argument::OPTIONAL, '参数')
            ->setDescription('足球总榜and篮球排名');
    }

    protected function execute(Input $input, Output $output)
    {

        $action = trim($input->getArgument('action'));
        if ($action === 'football') {
            $this->model = new FootballCompetitions(); // //足球联赛表
        } else {
            $this->model = new BasketballCompetitions();  //篮球联赛表
        }
        $this->model->chunk(100, function ($models) use ($action, $output) {
            foreach ($models as $model) {
                $output->writeln('============进============');
                $seaon = MatchesSeason::field('season_id')->where(['competition_id' => $model['api_id'], 'type' => $action, 'is_current' => 1])->find();//年份 is_current = 1 是默认本年度  爬取接口的最新年份
                if (!empty($seaon)) {
                    $url = 'https://api-f.kqiu.cn/api/v1/' . $action . '/competition/table-list?competition_id=' . $model['api_id'] . '&season_id=' . $seaon["season_id"]; //年份
                    $res = curl_get($url);
                    $arr = json_decode($res, true);
                    if ($arr['code'] == 0) {
                        $new_arr['competition_id'] =  $model['api_id'];
                        $new_arr['foreign_key_id'] = $model['id'];
                        if($action == 'football'){
                            $new_arr['knockout'] = $arr['data']['knockout'] == true ? 1 : 0;
                            $new_arr['is_group'] = $arr['data']['is_group'] == true ? 1 : 0;
                            if ($arr['data']['is_group']) { //是true 的时候tables 是null
                                $output->writeln('============is_groupTrue============');
                                $stage_list = json_encode($arr['data']['stage_list'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                                $groups = json_encode($arr['data']['groups'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                                $new_arr['tables_all'] = null;
                                $new_arr['tables_home'] = null;
                                $new_arr['tables_away'] = null;
                            } else {
                                if (!empty($arr['data']['tables'])) {
                                    $output->writeln('============is_groupFalse============');
                                    if (array_key_exists('all', $arr['data']['tables']) && !empty($arr['data']['tables']['all'])) {
                                        $new_arr['tables_all'] = json_encode($arr['data']['tables']['all'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                                    } else {
                                        $new_arr['tables_all'] = null;
                                    }
                                    if (array_key_exists('home', $arr['data']['tables']) && !empty($arr['data']['tables']['home'])) {
                                        $new_arr['tables_home'] = json_encode($arr['data']['tables']['home'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                                    } else {
                                        $new_arr['tables_home'] = null;
                                    }
                                    if (array_key_exists('away', $arr['data']['tables']) && !empty($arr['data']['tables']['away'])) {
                                        $new_arr['tables_away'] = json_encode($arr['data']['tables']['away'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                                    } else {
                                        $new_arr['tables_away'] = null;
                                    }
                                }
                                $stage_list = null;
                                $groups = null;
                            }
                            $new_arr['stage_list'] = $stage_list;
                            $new_arr['groups'] = $groups;
                            $output->writeln('============' . $model->id . '============');
                            $is_find = FootballCompetitionTable::where(['foreign_key_id' => $model->id])->find();
                            if (empty($is_find)) {
                                FootballCompetitionTable::create($new_arr);
                                $str = 'add';
                            } else {
                                //更新
                                $is_find->save($new_arr);
                                $str = 'update';
                            }
                            $output->writeln('=========================Football' . $str . '完成=====================================');
                        }else{
                            $new_arr['has_playoff'] = $arr['data']['has_playoff'] == true ? 1 : 0;
                            if (array_key_exists('list', $arr['data']) && !empty($arr['data']['list'])) {
                                $new_arr['list'] = json_encode($arr['data']['list'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                            } else {
                                $new_arr['list'] = null;
                            }
                            if (array_key_exists('tabs', $arr['data']) && !empty($arr['data']['tabs'])) {
                                $new_arr['tabs'] = json_encode($arr['data']['tabs'], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
                            } else {
                                $new_arr['tabs'] = null;
                            }
                            $output->writeln('============' . $model->id . '============');
                            $is_find = BasketballCompetitionTable::where(['foreign_key_id' => $model->id])->find();
                            if (empty($is_find)) {
                                BasketballCompetitionTable::create($new_arr);
                                $str = 'add';
                            } else {
                                //更新
                                $is_find->save($new_arr);
                                $str = 'update';
                            }
                            $output->writeln('=========================BasketBall' . $str . '完成=====================================');
                        }
                    } else {
                        $output->writeln('api参数出错');
                    }
                } else {
                    $output->writeln('年份无法获取');
                }
            }
        });

        //https://api-f.kqiu.cn/api/v1/football/competition/table-list?competition_id=100567&season_id=20240 //总榜
        //https://api-f.kqiu.cn/api/v1/football/competition/fixtures?competition_id=100567&season_id=20240 赛程
        //https://api-f.kqiu.cn/api/v1/football/competition/player-rank?competition_id=100567&key=k_goals&season_id=20240 //球员榜
        //https://api-f.kqiu.cn/api/v1/football/competition/team-rank?competition_id=100567&season_id=20240&key=k_goals //球队榜

        //https://api-f.kqiu.cn/api/v1/basketball/competition/table-list?competition_id=300001&season_id=20231 //排名
        //https://api-f.kqiu.cn/api/v1/basketball/competition/fixtures?competition_id=300001&season_id=20231 赛程
        //https://api-f.kqiu.cn/api/v1/basketball/competition/player-rank?competition_id=300001&key=k_points&season_id=20231 球员榜
        //https://api-f.kqiu.cn/api/v1/basketball/competition/team-rank?competition_id=300001&season_id=20231&key=k_points //球队榜
        // 指令输出
        $output->writeln('competition');
    }
}
